package com.yantong.service.impl;

import com.yantong.constant.NoticeShowState;
import com.yantong.utils.PageUtils;
import com.yantong.pojo.Notice;
import com.yantong.mapper.NoticeMapper;
import com.yantong.service.NoticeService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author kanghaixin
 * @date 2018/8/29.20:10
 */
@Service
public class NoticeServiceImpl implements NoticeService {

    private static final Logger logger = LoggerFactory.getLogger(NoticeServiceImpl.class);
    @Autowired
    private NoticeMapper noticeMapper;



    @Override
    public PageUtils<Notice> listNotices(String title, Long centreId, int offset, int limit) {
        PageUtils<Notice> pageUtils = new PageUtils<>();
        Map<String,Object> params = new HashMap<>(2);
        if (StringUtils.isNotEmpty(title)) {
            params.put("title", title);
        }
        if (centreId != null) {
            params.put("centreId", centreId);
        }
        int total = noticeMapper.countByParams(params);
        if (total >0) {
            pageUtils.setRows(noticeMapper.listByParams(params, new RowBounds(offset, limit)));
            pageUtils.setTotal(total);
        }
        return pageUtils;
    }

    @Override
    public List<Notice> getAllListNotices(Long centreId) {
        List<Notice> noticeList=null;
        Map<String,Object> params = new HashMap<>(2);
        if (centreId != null) {
            params.put("centreId", centreId);
        }
        params.put("display", 1);
        int total = noticeMapper.countByParams(params);
        if (total >0) {
            noticeList=noticeMapper.listByParams0(params);
        }
        return noticeList;
    }

    @Override
    public PageUtils<Notice> listNotices(Long centreId, int offset, int limit) {
        PageUtils<Notice> pageUtils = new PageUtils<>();
        Map<String,Object> params = new HashMap<>(2);
        if (centreId != null) {
            params.put("centreId", centreId);
        }
        params.put("display", 1);
        int total = noticeMapper.countByParams(params);
        if (total >0) {
            pageUtils.setRows(noticeMapper.listByParams(params, new RowBounds(offset, limit)));
            pageUtils.setTotal(total);
        }
        return pageUtils;
    }

    @Override
    public int saveNotice(Notice notice) {
        if (notice == null) {
            return 0;
        }
        return noticeMapper.insertSelective(notice);
    }

    @Override
    public int disableShow(Long noticeId, Long aid, String aname) {
        if (noticeId == null) {
            return 0;
        }
        Notice notice = new Notice();
        notice.setId(noticeId);
        notice.setDisplay(NoticeShowState.disbaled);
        notice.setAname(aname);
        notice.setAid(aid);
        return noticeMapper.updateByPrimaryKeySelective(notice);
    }

    @Override
    public int enableShow(Long noticeId, Long aid, String aname) {
        if (noticeId == null) {
            return 0;
        }
        Notice notice = new Notice();
        notice.setId(noticeId);
        notice.setDisplay(NoticeShowState.enabled);
        notice.setAname(aname);
        notice.setAid(aid);
        return noticeMapper.updateByPrimaryKeySelective(notice);
    }

    @Override
    public Notice getByKey(Long noticeId) {
        if (noticeId == null) {
            return null;
        }
        return noticeMapper.selectByPrimaryKey(noticeId);
    }

    @Override
    public int updateNotice(Notice notice) {
        if (notice == null || notice.getId() == null) {
            return 0;
        }
        if (notice.getDisplay() == null) {
            notice.setDisplay(0);
        }
        return noticeMapper.updateByPrimaryKeySelective(notice);
    }

    @Override
    public int batchRemove(List<Long> noticeIds) {
        if (CollectionUtils.isEmpty(noticeIds)) {
            return 0;
        }
        return noticeMapper.batchRemove(noticeIds);
    }
}
